package lablayer.view.state;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Darkness
 */

class State implements StateInterface {
    private StateImp stateImp = null;
    private List<StateImp> stateList = new ArrayList<StateImp>();

    private State() {
        super();
    }

    public State(StateImp imp) {
        this();
        this.stateList.add(imp);
        this.stateImp = imp;
    }

    @Override
    public int getStateId() {
        return this.stateImp.getStateId();
    }

    @Override
    public void setStateId(int stateId) {
        Iterator<StateImp> stateIterator = this.stateList.iterator();
        boolean isError = true;
        while (stateIterator.hasNext()) {
            StateImp state = stateIterator.next();
            if (state.getStateId() == stateId) {
                this.stateImp = state;
                isError = false;
                break;
            }
        }
        if (isError) {
            //throw new UnsupportedOperationException("setStateId(): can't find state id");
        }
    }

    @Override
    public void addState(int stateId) {
        switch (stateId) {
            case StateInterface.COMPONENT_STATE: {
                this.stateList.add(new ComponentState());
                break;
            }
            case StateInterface.LINK_STATE: {
                this.stateList.add(new LinkState());
            }
        }
    }

    @Override
    public void removeState(int stateId) {
        Iterator<StateImp> stateIterator = this.stateList.iterator();
        boolean isError = true;
        while (stateIterator.hasNext()) {
            StateImp state = stateIterator.next();
            if (state.getStateId() == stateId) {
                this.stateList.remove(state);
                isError = false;
                break;
            }            
        }
        if (isError) {
            //throw new UnsupportedOperationException("removeState(): can't find state id");
        }
    }

}
